package com.bhs.sansonglogistics.ui.message;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseFragment;
import com.bhs.sansonglogistics.bean.MessageBean;
import com.bhs.sansonglogistics.bean.MessageCount;
import com.bhs.sansonglogistics.bean.event.CircleRefresh;
import com.bhs.sansonglogistics.bean.event.MessageCenterRefresh;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.MessageAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 消息中心
 */
public class MessageCenterFragment extends BaseFragment implements NetCallBack {

    private RecyclerView mRvSystemMessages;
    private List<MessageBean> list = new ArrayList<>();
    private MessageAdapter messageAdapter;
    private RecyclerView mRvMessage;

    public static MessageCenterFragment newInstance() {
        return new MessageCenterFragment();
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_message_center;
    }

    @Override
    public void initView(View root) {
        EventBus.getDefault().register(this);
        mRvSystemMessages = root.findViewById(R.id.rv_system_messages);
        mRvMessage = root.findViewById(R.id.rv_message);
        list.add(new MessageBean(R.mipmap.ic_visitor, "访客记录"));
        list.add(new MessageBean(R.mipmap.ic_logistics_information, "物流信息", "1"));
        list.add(new MessageBean(R.mipmap.ic_welfare, "红包福利", "3"));
        list.add(new MessageBean(R.mipmap.ic_inform, "服务通知", "2"));
        messageAdapter = new MessageAdapter(R.layout.item_message, list);
        mRvSystemMessages.setAdapter(messageAdapter);
        mRvSystemMessages.setLayoutManager(new LinearLayoutManager(getContext()));
        messageAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (position == 0) {
                    startActivity(new Intent(getActivity(), VisitorLogActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), SystemMessagesActivity.class)
                            .putExtra("title", list.get(position).getTitle())
                            .putExtra("type", list.get(position).getType()));
                }
            }
        });

    }

    @Override
    public void initData() {
        networkRequest(netApi.get_message_num("logistics"), this);
    }




    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        MessageCount messageCount = new Gson().fromJson(json, MessageCount.class);
        if (messageCount.getCode().equals("1")) {
            list.get(0).setContent(messageCount.getData().getType_0().getContent());
            list.get(0).setCreate_time(messageCount.getData().getType_0().getCreate_time());

            list.get(1).setCount(messageCount.getData().getType_1().getNum());
            list.get(1).setContent(messageCount.getData().getType_1().getContent());
            list.get(1).setCreate_time(messageCount.getData().getType_1().getCreate_time());

            list.get(2).setCount(messageCount.getData().getType_3().getNum());
            list.get(2).setContent(messageCount.getData().getType_3().getContent());
            list.get(2).setCreate_time(messageCount.getData().getType_3().getCreate_time());

            list.get(3).setCount(messageCount.getData().getType_2().getNum());
            list.get(3).setContent(messageCount.getData().getType_2().getContent());
            list.get(3).setCreate_time(messageCount.getData().getType_2().getCreate_time());
            messageAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }

    @Subscribe
    public void onEvent(MessageCenterRefresh event) {
        networkRequest(netApi.get_message_num("logistics"), this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}